/**
  @file: FileListReader.java

  PURPOSE: 
  Implement an object to read and parse lines from a text file containing
  names of files to be transfered to a remote host.
  
  @author Bill Kraemer

  REVISION HISTORY ABSTRACT

  PROGRAMMER:  Bill Kraemer         ORG: MSE

  Copyright 2011 Mission Solutions LLC. All rights reserved.

  END ABSTRACT
 */
package com.mse.utilities;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
  DESCRIPTION:
  This class implements an object to read and parse lines from a text file 
  containing names of files to be transfered to a remote host. The file must 
  contain one line for each file to be transfered. Each line should be 
  formatted as: 
  <p>
  <pre>
  srcPath [destDir]
  </pre>
  where "srcPath" is the full path to a file to be transfered, and "destDir" 
  is an optional destination directory path. 
  <p>
  The Java Properties file format is supported, so comments, blank lines, 
  and "=" delimiters are permitted. However, key names are discarded.
 */
class FileListReader {
   
   ///< a "bean" to contain token values parsed from the line
   public class LineTokens {
      public String srcPath;
      public String destDir; 
   }
   
   ///< a buffered reader for the file
   private BufferedReader bufferedReader;
   
   /**
     DESCRIPTION:
     Constructs an instance of FileListReader.
   
     @param fileListPath the name of the file to read.
     @throws FileNotFoundException if the file cannot be opened.
    */
   FileListReader(String fileListPath) throws FileNotFoundException {
      this.bufferedReader = new BufferedReader(new FileReader(fileListPath));
   }
   
   /**
     DESCRIPTION:
     This method reads a line of text from the file, skipping blank lines
     and comment lines, and returns the token values parsed from the text.
   
     @return Token values parsed from the line.
     @throws IOException if an error occurs reading the file.
    */
   public LineTokens readLine() throws IOException {
      
      LineTokens tokens = null;

      // read a line of text
      String line = null;
      do {
         // read the next line of text from the file
         line = bufferedReader.readLine();
         
         // null = end of file
         if (line == null) break;
         
         // trim whitespace
         line = line.trim();
         
         // skip blank lines and comment lines
      } while (line.isEmpty() || line.startsWith("#"));

      // process the line of text
      if (line != null) {
      
         // support Java properties: discard chars preceding the first "="
         String split[] = line.split("=",2);
         if (split.length < 2)
            line = split[0];
         else
            line = split[1];
         
         // split the line at the first space to extract optional destDir
         split = line.split(" ",2);
         
         // save the token values from the line
         tokens = new LineTokens();
         tokens.srcPath = split[0].trim();
         tokens.destDir = null;
         if (split.length > 1) tokens.destDir = split[1].trim();
      }
   
      // return the token values
      return tokens;
   }
   
   /**
     DESCRIPTION:
     This method closes the file.
    */
   public void close() {
      try {
         this.bufferedReader.close();
      } catch (IOException e) {
         // ignore any exception
      }
   }

}
